package com.astar.service.impl;

import com.astar.entity.CustomUser;
import com.astar.entity.PractiseCourseType;
import com.astar.entity.User;
import com.astar.mapper.PractiseCourseTypeMapper;
import com.astar.service.IPractiseCourseTypeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 分类练习-科目类别表  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2020-01-31
 */
@Service
public class PractiseCourseTypeServiceImpl extends ServiceImpl<PractiseCourseTypeMapper, PractiseCourseType> implements IPractiseCourseTypeService {


    @Autowired
    private PractiseCourseTypeMapper practiseCourseTypeMapper;

    @Autowired
    HttpServletRequest request; //这里可以获取到request


    @Override
    public List<PractiseCourseType> selectAll(String name) {
        if (name==null){
            name="";
        }
        QueryWrapper<PractiseCourseType> queryWrapper = new QueryWrapper<PractiseCourseType>();
        queryWrapper.like("COURSE_NAME",name);
        List<PractiseCourseType> practiseCourseTypes = practiseCourseTypeMapper.selectList(queryWrapper);
        return practiseCourseTypes;
    }

    @Override
    public PractiseCourseType selectForId(Integer id) {
        return practiseCourseTypeMapper.selectById(id);

    }


    @Override
    public int deleteForId(PractiseCourseType practiseCourseType) {
        if (practiseCourseType!=null){
            if (practiseCourseType.getStatus().equals("Y")){
                practiseCourseType.setStatus("N");
            }else if (practiseCourseType.getStatus().equals("N")){
                practiseCourseType.setStatus("Y");
            }
        }
        return practiseCourseTypeMapper.updateById(practiseCourseType);
    }

    @Override
    public int deleteForIds(PractiseCourseType practiseCourseType) {
        practiseCourseType.setStatus("N");
        return practiseCourseTypeMapper.updateById(practiseCourseType);
    }

    @Override
    public int add(PractiseCourseType practiseCourseType) {

        HttpSession session= request.getSession();
        List<CustomUser> user = (List<CustomUser>) session.getAttribute("menus");

        practiseCourseType.setCreatedBy(user.get(0).getLoginName());
        LocalDateTime localDateTime3 = LocalDateTime.now();
        practiseCourseType.setCreatedTime(localDateTime3);
        return practiseCourseTypeMapper.insert(practiseCourseType);

    }

    @Override
    public int updateForId(PractiseCourseType practiseCourseType) {
        HttpSession session= request.getSession();
        List<CustomUser> user = (List<CustomUser>) session.getAttribute("menus");

        practiseCourseType.setCreatedBy(user.get(0).getLoginName());
        LocalDateTime localDateTime3 = LocalDateTime.now();
        practiseCourseType.setCreatedTime(localDateTime3);
        return practiseCourseTypeMapper.updateById(practiseCourseType);
    }

}
